package com.zhuangjie.user.controller;


import com.zhaungjie.base.conduit.R;
import com.zhuangjie.user.entity.AddressBook;
import com.zhuangjie.user.service.AddressBookService;
import com.zhuangjie.user.vo.AddressBookVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * <p>
 * 收货地址表 前端控制器
 * </p>
 *
 * @author zhuangjie
 * @since 2023-10-15
 */
@RestController
@RequestMapping("/address-book")
@Api("收货地址相关API")
public class AddressBookController {
    @Autowired
    private AddressBookService addressBookService;
    @ApiOperation("添加或更新地址")
    @PostMapping("/save")
    public R saveAddress(@RequestBody AddressBook addressBook) {
        return R.capture(()->addressBookService.saveAddress(addressBook));
    }
    @ApiOperation("根据id删除地址")
    @DeleteMapping("/delete/{addressBookId}")
    public R delete(@NotNull(message = "地址的id不能为空") @PathVariable("addressBookId") Long addressBookId) {
        return R.capture(()->addressBookService.removeUserAddressBookById(addressBookId));
    }

    @ApiOperation("获取用户列表")
    @GetMapping("/user-address-list")
    public R userAddressList() {
        List<AddressBook> addressBookList = addressBookService.getUserAddressList();
        return R.ok().data(addressBookList);
    }

    @ApiOperation("根据id获取当前用户地址信息")
    @GetMapping("/{addressBookId}")
    public R getAddressBookById(@PathVariable("addressBookId") Long addressBookId) {
        AddressBook addressBook = addressBookService.getUserAddressBookById(addressBookId);
        return R.require(addressBook); // return AddressBook
    }

    @ApiOperation("根据店铺的经纬度信息获取具有是否合理属性返回列表")
    @GetMapping("/preferred-address-list/{shopId}")
    public R preferredAddressList(@NotNull(message = "店铺id不能为空") @PathVariable("shopId") Long shopId) throws Exception {
        List<AddressBookVO> addressBookList =  addressBookService.preferredAddressList(shopId);
        return R.ok().data(addressBookList);
    }
    @ApiOperation("根据店铺id获取用户合适的地址，如果没有适合的将返回null")
    @GetMapping("/preferred-address/{shopId}")
    public R preferredAddress(@NotNull(message = "店铺id不能为空") @PathVariable("shopId") Long shopId) throws Exception {
        AddressBook addressBook =  addressBookService.preferredAddress(shopId);
        return R.ok().data(addressBook);
    }

    @ApiOperation("根据请求携带的店铺经纬度，获取合适的地址，如果没有适合的将返回null")
    @GetMapping("/preferred-address")
    public R preferredAddress(@NotNull(message = "需要传入经度") @RequestParam("longitude") Double longitude,
                              @NotNull(message = "需要传入纬度") @RequestParam("latitude") Double latitude,
                              @NotNull(message = "需要输入店铺经营范围")  @RequestParam("businessScope") Double businessScope ) throws Exception {
        return R.ok().data(addressBookService.preferredAddress(longitude,latitude,businessScope));
    }



}

